package com.example.demoforspringboot.web;

import com.example.demoforspringboot.entity.User;
import com.example.demoforspringboot.entity.UserPasswordEdit;
import com.example.demoforspringboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @GetMapping("/get")
    public String greeting(){
        return "cc so ...hhh";
    }

    @GetMapping("/getById/{id}/{name}")
    public User getById(@PathVariable("id") Integer id,@PathVariable("name") String name){
        User user = new User();
        user.setId(id);
        user.setName(name);
        return user;
    }

    @GetMapping("/getByIds")
    public User getById(@RequestParam("id") Integer id,@RequestParam("name") String name){
        User user = new User();
        user.setId(id);
        user.setName(name);
        return user;
    }

    @PostMapping("getByIdss")
    public User getByIdss(Integer id) {
        User user = new User();
        user.setId(id);
        user.setName("张三");
        return user;
    }

    @PostMapping("/add")
    public User add(@RequestBody User user) throws Exception {
        userService.save(user);
        return user;
    }

    @GetMapping("getByIdFromDb/{id)")
    public User getByIdFromDb(@PathVariable("id") Integer id) throws Exception {
        User user = userService.getById(id);
        return user;
    }

    @GetMapping("/getByIdFromDb2")
    public User getByIdFromDb(@RequestParam("id") Integer id) throws Exception {
        User user = userService.getById2(id);
        return user;
    }

    @PostMapping("/passwordEdit")
    public User passwordEdit(@RequestBody UserPasswordEdit userPasswordEdit){
        User user = userService.update(userPasswordEdit);
        return user;
    }
}
